package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysThali;
import com.ruoyi.system.domain.UserTask;
import com.ruoyi.system.domain.UserThali;
import com.ruoyi.system.mapper.SysThaliMapper;
import com.ruoyi.system.mapper.UserThaliMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserFundsMapper;
import com.ruoyi.system.domain.UserFunds;
import com.ruoyi.system.service.IUserFundsService;

/**
 * 用户资金Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-05
 */
@Service
public class UserFundsServiceImpl implements IUserFundsService 
{
    @Autowired
    private UserFundsMapper userFundsMapper;
    @Autowired
    private UserThaliMapper userThaliMapper;
    @Autowired
    private SysThaliMapper sysThaliMapper;
    /**
     * 查询用户资金
     * 
     * @param id 用户资金主键
     * @return 用户资金
     */
    @Override
    public UserFunds selectUserFundsById(Long id)
    {
        return userFundsMapper.selectUserFundsById(id);
    }

    /**
     * 查询用户资金列表
     * 
     * @param userFunds 用户资金
     * @return 用户资金
     */
    @Override
    public List<UserFunds> selectUserFundsList(UserFunds userFunds)
    {
        return userFundsMapper.selectUserFundsList(userFunds);
    }

    /**
     * 新增用户资金
     * 
     * @param userFunds 用户资金
     * @return 结果
     */
    @Override
    public int insertUserFunds(UserFunds userFunds)
    {
        userFunds.setCreateTime(DateUtils.getNowDate());
        return userFundsMapper.insertUserFunds(userFunds);
    }

    /**
     * 修改用户资金
     * 
     * @param userFunds 用户资金
     * @return 结果
     */
    @Override
    public int updateUserFunds(UserFunds userFunds)
    {
        userFunds.setUpdateTime(DateUtils.getNowDate());
        return userFundsMapper.updateUserFunds(userFunds);
    }

    /**
     * 批量删除用户资金
     * 
     * @param ids 需要删除的用户资金主键
     * @return 结果
     */
    @Override
    public int deleteUserFundsByIds(Long[] ids)
    {
        return userFundsMapper.deleteUserFundsByIds(ids);
    }

    /**
     * 删除用户资金信息
     * 
     * @param id 用户资金主键
     * @return 结果
     */
    @Override
    public int deleteUserFundsById(Long id)
    {
        return userFundsMapper.deleteUserFundsById(id);
    }

    @Override
    public int updateUserFundsByTask(UserTask userTask) {
        /**
         * 根据用户的时长扣费
         */
        //根据任务信息获得用户余额信息和用户的套餐信息
        UserThali userThali = userThaliMapper.selectUserThaliByUserId(Long.parseLong(userTask.getCreateBy()));
        //套餐有效且未过期
        DecimalFormat df = new DecimalFormat("#.00");
        String funs = "";
        if (userThali != null && "1".equals(userThali.getRemake())) {
            SysThali sysThali = sysThaliMapper.selectSysThaliById(userThali.getThaliId());
            BigDecimal paidPoints = new BigDecimal(Double.parseDouble(userTask.getPaidPoint()));
            BigDecimal price = sysThali.getPrice();
            BigDecimal result = paidPoints.divide(new BigDecimal(3), BigDecimal.ROUND_HALF_UP).multiply(price);
            funs = df.format(result);
        }else {
            //无套餐或者套餐过期用户
            SysThali system = sysThaliMapper.selectSysThaliById(Long.parseLong("2"));
            BigDecimal paidPoints = new BigDecimal(Double.parseDouble(userTask.getPaidPoint()));
            BigDecimal price = system.getPrice();
            BigDecimal result = paidPoints.divide(new BigDecimal(3), BigDecimal.ROUND_HALF_UP).multiply(price);
            funs = df.format(result );
        }
        UserFunds userFunds = userFundsMapper.selectUserFundsByUserId(userTask.getCreateBy());
        userFunds.setReduce(funs);
        userFunds.setReduceReason("任务："+userTask.getName()+"消耗："+funs+"米粒");
        userFunds.setUserId(Long.parseLong(userTask.getCreateBy()));
        userFunds.setCreateTime(DateUtils.getNowDate());
        userFunds.setUpdateTime(DateUtils.getNowDate());
        //计算可用余额，前期允许用户欠费100块，相当于免费赠送用户100块。
        BigDecimal da =new BigDecimal(Double.parseDouble(userFunds.getAvailable()));
        BigDecimal duf = new BigDecimal(Double.parseDouble(funs));
        userFunds.setAvailable(df.format(da.subtract(duf)));
        userFunds.setIncrease("");
        userFunds.setIncreaseReason("");
        return userFundsMapper.insertUserFunds(userFunds);
    }
}